package com.wb.adutils.ads.gmad

import java.lang.Exception
import android.app.Activity
import android.view.ViewGroup
import com.bytedance.msdk.api.AdError
import com.bytedance.msdk.api.reward.RewardItem
import com.bytedance.msdk.api.v2.ad.reward.GMRewardAd
import com.bytedance.msdk.api.v2.ad.reward.GMRewardedAdListener
import com.bytedance.msdk.api.v2.ad.reward.GMRewardedAdLoadCallback
import com.bytedance.msdk.api.v2.slot.GMAdOptionUtil
import com.bytedance.msdk.api.v2.slot.GMAdSlotRewardVideo
import com.wb.adutils.AdBaseLoader
import com.wb.adutils.AdUtils
import com.wb.adutils.db.AdEntity
import com.wb.adutils.tools.AdConstants

import com.wb.adutils.tools.LogC


class GMRewardAdUtils(val adEntity: AdEntity?): AdBaseLoader(adEntity) {

    private var mttRewardAd: GMRewardAd? = null

    override fun excLoadAd(activity: Activity) {
        try {
            if (isAdLoadSuccess && mttRewardAd?.isReady == true) {
                LogC.e("激励 -> 使用缓存广告")
                mttRewardAd?.showRewardAd(activity)
                return
            }
            LogC.e("激励 -> 开始请求激励广告")
            isAdLoadSuccess = false
            val adSlotRewardVideo = GMAdSlotRewardVideo.Builder()
                .setVolume(0.2f) //配合Admob的声音大小设置[0-1]
                .setGMAdSlotGDTOption(GMAdOptionUtil.getGMAdSlotGDTOption().build())
                .setOrientation(orientation) //必填参数，期望视频的播放方向：GMAdConstant.HORIZONTAL 或 GMAdConstant.VERTICAL
                .setBidNotify(true)
                .setUserID(AdUtils.getDeviceId(activity))//用户id,必传参数
                .build()
            mttRewardAd = GMRewardAd(activity, adEntity?.adUnit)

            //请求广告
            mttRewardAd?.loadAd(adSlotRewardVideo, object : GMRewardedAdLoadCallback {
                override fun onRewardVideoLoadFail(adError: AdError) {
                    LogC.e("激励 -> onRewardVideoLoadFail：" + adError.message)
                    excAdLoadFail(adError.code)
                }

                override fun onRewardVideoAdLoad() {
                }

                override fun onRewardVideoCached() {
                    if (timeoutDisposable?.isDisposed == true) { // 已超时
                        LogC.e("激励 -> onRewardVideoCached...已超时")
                        return
                    }
                    LogC.e("激励 -> onRewardVideoCached...")
                    timeoutDisposable?.dispose()
                    isAdLoadSuccess = true
                    listener?.onAdLoaded(null)
                }
            })
        } catch (e: Exception) {
            e.printStackTrace()
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_EXCEPTION)
            release()
        }
    }

    override fun showAd(adRoot: ViewGroup?) {
        LogC.e("激励 -> showAd...")
        val activity = getActivity()
        if (activity == null) {
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_ACTIVITY_IS_NULL)
            release()
            return
        }

        if (isAdLoadSuccess && mttRewardAd?.isReady == true) {
            LogC.e("激励 -> showAd...显示")
            val rewardListener = MyGMRewardAdListener()
            mttRewardAd?.setRewardAdListener(rewardListener)
            mttRewardAd?.setRewardPlayAgainListener(rewardListener)
            mttRewardAd?.showRewardAd(activity)
        } else {
            timeoutDisposable?.dispose()
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_NOT_LOAD_SUCCESS)
            release()
        }
    }



    inner class MyGMRewardAdListener : GMRewardedAdListener {
        private var isRewardVerify = false
        override fun onRewardedAdShow() {
            LogC.e("激励 -> showAd...onRewardedAdShow...")
            listener?.onAdDisplay()
        }

        override fun onRewardedAdShowFail(p0: AdError) {
            LogC.e("激励 -> showAd...onRewardedAdShowFail..." + p0.message)
            listener?.onAdLoaded(null)
            release()
        }

        override fun onRewardClick() {
            LogC.e("激励 -> showAd...onRewardClick...")
            listener?.onAdClicked()
        }

        override fun onRewardedAdClosed() {
            LogC.e("激励 -> showAd...onRewardedAdClosed...")
            mttRewardAd = null
            listener?.onAdComplete(isRewardVerify)
            release()
        }

        override fun onVideoComplete() {
        }

        override fun onVideoError() {
            LogC.e("激励 -> showAd...onVideoError...")
            listener?.onAdLoadFail(AdConstants.AD_ERROR_CODE_INTERNAL)
            release()
        }

        override fun onRewardVerify(p0: RewardItem) {
            LogC.e("激励 -> showAd...onRewardVerify...")
            isRewardVerify = true
        }

        override fun onSkippedVideo() {
            LogC.e("激励 -> showAd...onSkippedVideo...")
        }

    }

    override fun release() {
        super.release()
        mttRewardAd?.destroy()
        mttRewardAd = null
    }


}